package com.ushine.web.template.quartz.controller;

import com.ushine.web.component.exception.ServiceException;
import com.ushine.web.component.http.HttpResponseVO;
import com.ushine.web.component.scheduled.model.QuartzJobDO;
import com.ushine.web.component.scheduled.QuartzService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author: yls
 * @Date 2022/2/18 14:19
 * @Description: 定时器任务调度控制
 * @Version 1.0
 **/
@RestController
@RequestMapping("component/quartz")
@Slf4j
@Api(tags = "组件模块-定时器调度")
public class QuartzController {
    @Resource
    private QuartzService quartzService;

    @ApiOperation("获取所有任务")
    @PostMapping(value = "list")
    public HttpResponseVO<QuartzJobDO> listQuartzJobs() throws SchedulerException {
        List<QuartzJobDO> quartzJobList = quartzService.listAllJobs();
        return new HttpResponseVO<QuartzJobDO>().ok("查询成功", quartzJobList);
    }

    @ApiOperation("保存任务")
    @PostMapping(value = "save")
    public HttpResponseVO<Object> save(@RequestBody @Valid QuartzJobDO param) {
        try {
            //jobClass 也就是具体的实现类例如：com.word.task.FoundEditorTask
            Class clazz = Class.forName(param.getJobClass());
            QuartzJobBean bean = (QuartzJobBean) clazz.newInstance();
            quartzService.insertJob(bean.getClass(),
                    param.getJobName(), param.getJobGroupName(),
                    param.getCronExpression(), param.getDescription(),
                    null);
        } catch (ClassNotFoundException e) {
            log.error("{}没有找到任务类{}", getClass(), param.getJobClass());
            return new HttpResponseVO<>().error("没有找到任务类");
        } catch (IllegalAccessException | InstantiationException | SchedulerException e2) {
            log.error("{}加载异常{}", getClass(), param.getJobClass());
            return new HttpResponseVO<>().error("没有找到任务类");
        }
        return new HttpResponseVO<>().ok("保存成功");
    }

    @ApiOperation("修改任务")
    @PostMapping(value = "update")
    public HttpResponseVO<Object> update(@RequestBody @Valid QuartzJobDO param) throws SchedulerException, ServiceException {
        Boolean flag = quartzService.updateJob(param);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("修改成功");
        }
        return new HttpResponseVO<>().error("修改失败");
    }

    @ApiOperation("删除任务")
    @PostMapping(value = "deleteJob")
    public HttpResponseVO<Object> deleteJob(@RequestBody @Valid QuartzJobDO param) throws SchedulerException {
        quartzService.deleteJob(param.getJobName(), param.getJobGroupName());
        return new HttpResponseVO<>().ok("删除成功");
    }

    @ApiOperation("暂停任务")
    @PostMapping(value = "stopJob")
    public HttpResponseVO<Object> stopJob(@RequestBody @Valid QuartzJobDO param) throws SchedulerException, ServiceException {
        Boolean flag = quartzService.pauseJob(param.getJobName(), param.getJobGroupName());
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("暂停成功");
        } else {
            return new HttpResponseVO<>().ok("暂停失败");
        }
    }

    @ApiOperation(value = "恢复任务")
    @PostMapping(value = "resumeJob")
    public HttpResponseVO<Object> resumeJob(@RequestBody @Valid QuartzJobDO param) throws SchedulerException, ServiceException {
        Boolean flag = quartzService.resumeJob(param.getJobName(), param.getJobGroupName());
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("恢复成功");
        }
        return new HttpResponseVO<>().error("恢复异常");

    }

}
